package ru.uskov.trainer.model.impl;

import java.math.BigDecimal;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import ru.uskov.trainer.model.IListboxItem;
import ru.uskov.trainer.model.IProductDescription;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class ProductDescription implements IListboxItem, IProductDescription {
  
  @PrimaryKey
  @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
  private Long id;
  
  @Persistent
  private String listItemId;
  @Persistent
  private BigDecimal carbohydrates;
  @Persistent
  private String name;
  @Persistent
  private BigDecimal kkals;
  @Persistent
  private BigDecimal fat;
  @Persistent
  private BigDecimal proteins;
  @Persistent
  private BigDecimal water;
  @Persistent
  private String groupId;

  ProductDescription(String id, String description, String groupId) {
    this.listItemId = id;
    this.groupId = groupId;
    String[] items = description.split("\t");
    if (items.length != 6) {
      throw new RuntimeException("Lenght = " + items.length + ". Expected 6. Illegal argument: " + description);
    }
    name = items[0];
    BigDecimal hundred = new BigDecimal("100");
    water = (new BigDecimal(items[1])).divide(hundred, 4, BigDecimal.ROUND_HALF_UP);
    proteins = (new BigDecimal(items[2])).divide(hundred, 4, BigDecimal.ROUND_HALF_UP);
    fat = (new BigDecimal(items[3])).divide(hundred, 4, BigDecimal.ROUND_HALF_UP);
    carbohydrates = (new BigDecimal(items[4])).divide(hundred, 4, BigDecimal.ROUND_HALF_UP);
    kkals = (new BigDecimal(items[5])).divide(hundred, 4, BigDecimal.ROUND_HALF_UP);

  }

  @Override
  public BigDecimal getCarbohydrates() {
    return carbohydrates;
  }

  @Override
  public BigDecimal getProteins() {
    return proteins;
  }

  @Override
  public BigDecimal getFat() {
    return fat;
  }

  @Override
  public BigDecimal getKKalls() {
    return kkals;
  }

  @Override
  public BigDecimal getWater() {
    return water;
  }

  @Override
  public String getName() {
    return name;
  }

  @Override
  public String getId() {
    return listItemId;
  }

  @Override
  public String getValue() {
    return name;
  }

  public Long getDBKey() {
    return id;
  }
  
  @Override
  public String toString() {
    return id + " -> " + name;
  }
}
